package com.example.tacocloudapi;

import com.example.tacoclouddata.TacoRepository;
import com.example.tacoclouddomain.Order;
import com.example.tacoclouddomain.Taco;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;

@RestController
@RequestMapping(path = "/design", produces = "application/json")
@CrossOrigin(origins = "*")
public class DesignTacoController {

    private TacoRepository tacoRepository;


    @Autowired
    EntityLinks entityLinks;

    public DesignTacoController(TacoRepository tacoRepository) {
        this.tacoRepository = tacoRepository;
    }

//    @GetMapping("/recent")
//    public Iterable<Taco> recentTacos(){
//        PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt").descending());
//        return tacoRepository.findAll(page).getContent();
//    }

//    @GetMapping("/recent")
//    public CollectionModel<EntityModel<Taco>> recentTacos(){
//        PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt").descending());
//        List<Taco> tacos = tacoRepository.findAll(page).getContent();
//        CollectionModel<EntityModel<Taco>> recentResources = CollectionModel.wrap(tacos);
////        recentResources.add(new Link("http://localhost:8080/design/recent", "recents"));
//        recentResources.add(
//                WebMvcLinkBuilder.linkTo(DesignTacoController.class)
//                .slash("recent")
//                .withSelfRel().withTitle("recents")
//        );
//        return recentResources;
//    }

    @GetMapping("/recent")
    public CollectionModel<TacoResource> recentTacos(){
        PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt").descending());
        List<Taco> tacos = tacoRepository.findAll(page).getContent();
        CollectionModel<TacoResource> tacoResources = new TacoResourceAssembler(DesignTacoController.class, TacoResource.class)
                .toCollectionModel(tacos);
        tacoResources.add(linkTo(methodOn(DesignTacoController.class)
                .recentTacos())
                .withRel("recents"));

        return tacoResources;
    }

    @GetMapping("/{id}")
    public ResponseEntity<Taco> tacoById(@PathVariable("id") long id){
        Optional<Taco> optTaco = tacoRepository.findById(id);
        if (optTaco.isPresent()){
            return new ResponseEntity<>(optTaco.get(), HttpStatus.OK);
        }
        return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
    }

    @PostMapping(consumes = "application/json")
    @ResponseStatus(HttpStatus.CREATED)
    public Taco postTaco(@RequestBody Taco taco){
        return tacoRepository.save(taco);
    }


}
